package it.trekker.upload;

import java.io.File;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.util.Log;

public class UploadHelperMultipart extends UploadHelper {

	public UploadHelperMultipart(Context ctx) {
		super(ctx);
	}

	@Override
	public boolean uploadFile(String filePath, String serverUrl) {
		File f = new File(filePath);
	    
		HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(serverUrl);
 
        try {
            MultipartEntity entity = new MultipartEntity();
 
//            entity.addPart("user", new StringBody("myUsername"));
//            entity.addPart("password", new StringBody("myPassword"));
            entity.addPart("fileUp", new FileBody(f));
            
            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost);
            //            InputStream is = response.getEntity().getContent();
            //            byte[] b = new byte[8];
            //            while(is.read(b) != -1)
            //            	Log.d(TAG, new String(b));
            
            if(response.getStatusLine().getStatusCode() == 200)
            	return true;
        }
        catch (ClientProtocolException e) {
            Log.d(TAG, e.getMessage(),e);
        }
        catch (IOException e) {
            Log.d(TAG, e.getMessage(),e);
        }
        
        return false;
	}
}
